<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * OrganisationMemberAnniversary Model
 *
 * @property Member $Member
 * @property OrganisationAnniversary $OrganisationAnniversary
 */
class OrganisationMemberImage extends CompositeKeyModel {
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";
    public $primaryKeys = array('id', 'organisation_id');
    
    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'OrganisationMember' => array(
            'className' => 'OrganisationMember',
            'foreignKey' => 'organisation_member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Organisation' => array(
            'className' => 'Organisation',
            'foreignKey' => 'organisation_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'MemberImage' => array(
            'className' => 'MemberImage',
            'foreignKey' => 'member_image_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );


    public function delete_images($org_mem_image_ids){
        $images = $this->updateAll(
            array('deleted' => 1),
            array(
                'OrganisationMemberImage.id' => $org_mem_image_ids,
            )
        );

       return $images;
    }


    public function getSearchOrgMembers($organisation_id,$term = ''){
        $query = "SELECT Member.id,Member.first_name,Member.middle_name,Member.last_name,OrganisationMember.id AS organisation_member_id,OrganisationMember.organisation_id
                FROM memberz.members AS Member
                LEFT JOIN organisations.organisation_members AS OrganisationMember ON OrganisationMember.member_id = Member.id
                WHERE OrganisationMember.organisation_id = $organisation_id AND
                ((Member.first_name LIKE '".$term."%') OR
                (Member.last_name LIKE '".$term."%') OR
                (Member.middle_name LIKE '".$term."%'))";
        $members = $this->query($query);
        return $members;
    }


    public function load_member_images($org_id,$filter = 'untagged',$search = false,$offset = 0,$limit = 18){
        $org_member_ids = array();
        if($search){
            $filtered_members = $this->getSearchOrgMembers($org_id,$search);
            foreach($filtered_members as $org_mem){
                $org_member_ids[] = $org_mem['OrganisationMember']['organisation_member_id'];
            }
        }
        $conditions = array('OrganisationMemberImage.organisation_id' => $org_id,'OrganisationMemberImage.deleted' => 0);
        
        if($filter == 'tagged'){
            $conditions['NOT'] = array(
                'OrganisationMemberImage.organisation_member_id' => null,
                'OrganisationMemberImage.member_image_id' => null
            );
        }
        
        else{
            $conditions['OrganisationMemberImage.organisation_member_id'] = null;
        }
        
        if($search){
            $conditions['OrganisationMemberImage.organisation_member_id'] = $org_member_ids;
        }
        
        $OrganisationMemberImage = ClassRegistry::init('OrganisationMemberImage');
        $images = $OrganisationMemberImage->find('all', array(
            'contain' => array(
                'MemberImage',
                'OrganisationMember'=>array(
                    'Member' => array(
                        'fields' =>array('Member.id','Member.full_name', 'Member.occupation', 'Member.title', 'Member.position', 'Member.dob')
                    )
                )
            ),
            'conditions' => $conditions,
            'order' => array('OrganisationMemberImage.id' => 'desc'),
            'limit' => $limit,
            'offset' => $offset
        ));

        return $images;
    }


    public function count_member_images($org_id,$filter = 'untagged',$search = false){
        $org_member_ids = array();
        if($search){
            $filtered_members = $this->getSearchOrgMembers($org_id,$search);
            foreach($filtered_members as $org_mem){
                $org_member_ids[] = $org_mem['OrganisationMember']['organisation_member_id'];
            }
        }
        $conditions = array('OrganisationMemberImage.organisation_id' => $org_id,'OrganisationMemberImage.deleted' => 0);
        if($filter == 'tagged'){
            $conditions['NOT'] = array('OrganisationMemberImage.organisation_member_id'=>null, 'OrganisationMemberImage.member_image_id'=>null);
        }
        else{
            $conditions['OrganisationMemberImage.organisation_member_id'] = null;
        }
        if($search){
            $conditions['OrganisationMemberImage.organisation_member_id'] = $org_member_ids;
        }

        $OrganisationMemberImage = ClassRegistry::init('OrganisationMemberImage');
        $count = $OrganisationMemberImage->find('count', array(
            'contain' => array(
                'MemberImage'=>array('fields'=>array('MemberImage.id','MemberImage.file_name','MemberImage.file_path','MemberImage.thumb_path')),
                'OrganisationMember'=>array(
                    'fields'=>array('OrganisationMember.id'),
                    'Member'=>array('fields'=>array('Member.id','Member.full_name'))
                )
            ),
            'conditions' => $conditions,
            'order' => array('OrganisationMemberImage.id' => 'desc'),
        ));

        return $count;
    }

}
